#!/usr/bin/env bash
# version: 1.0
# date: 2023/01/24
# 依赖包: bc【需要进行 小数计算】
# 安装命令: sudo pacman -Sy bc

get_cpu_mhz(){
    cpu_num=`cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq|wc -l`
    cpu_mhz_file='/tmp/.cpu-mhz.log'
    cpu_mhz_to_file=`cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq > $cpu_mhz_file`

    for i in `seq $cpu_num`; do
        # echo $i # 输出当前计算的 cpu核心数
        x_cpu_mhz=`cat $cpu_mhz_file | head -n $i | tail -n 1`

        for j in $x_cpu_mhz
        do
            # echo "j= $j" # 输出当前核心的 cpu频率
            ((sum += j))
        done
    done
    # echo "sum = $sum"

    # mhz=`echo "$sum / $cpu_num * 0.000001" | bc`  # 不去除小数点
    mhz=`echo "scale=2; ($sum / $cpu_num * 0.000001) / 1" | bc`  # 去小数点后两位
    # echo "mhz = $mhz"

    rm -f $cpu_mhz_file
}

get_cpu_mhz
echo $mhz